﻿using System.Collections.Generic;
using System.Threading.Tasks;
using WeiboWPSdk.Json;
using WeiboWPSdk.ResponseEntity;

namespace WeiboWPSdk.Api {
    public class SearchApi : ApiBase {
        public SearchApi(ClientHttpHelper httpHelper)
            : base(httpHelper) {
        }

        #region 搜索联想接口
        /// <summary>
        /// 搜索用户时的联想搜索建议
        /// http://open.weibo.com/wiki/2/search/suggestions/users
        /// </summary>
        /// <param name="q">搜索的关键字，必须做URLencoding。</param>
        /// <param name="count">返回的记录条数，默认为10。</param>
        /// <returns></returns>
        public async Task<IEnumerable<User>> GetSuggestionsUsersAsync(string q, int count = 10) {
            var json = await HttpHelper.GetBasicCommandAsync("search/suggestions/users", new WeiboParameter("q", q), new WeiboParameter("count", count));
            return JsonConvert.DeserializeObject<IEnumerable<User>>(json);
        }
        /// <summary>
        /// 搜索学校时的联想搜索建议
        /// http://open.weibo.com/wiki/2/search/suggestions/schools
        /// </summary>
        /// <param name="q">搜索的关键字，必须做URLencoding。</param>
        /// <param name="count">返回的记录条数，默认为10。</param>
        /// <param name="type">学校类型，0：全部、1：大学、2：高中、3：中专技校、4：初中、5：小学，默认为0。</param>
        /// <returns></returns>
        public async Task<IEnumerable<School>> GetSuggestionsSchoolsAsync(string q, int count = 10, int type = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("search/suggestions/schools", new WeiboParameter("q", q), new WeiboParameter("count", count), new WeiboParameter("type", type));
            return JsonConvert.DeserializeObject<IEnumerable<School>>(json);
        }
        /// <summary>
        /// @用户时的联想建议
        /// http://open.weibo.com/wiki/2/search/suggestions/at_users
        /// </summary>
        /// <param name="q">搜索的关键字，必须做URLencoding。</param>
        /// <param name="type">联想类型，0：关注、1：粉丝。</param>
        /// <param name="count">返回的记录条数，默认为10，粉丝最多1000，关注最多2000。</param>
        /// <param name="range">联想范围，0：只联想关注人、1：只联想关注人的备注、2：全部，默认为2。</param>
        /// <returns></returns>
        public async Task<IEnumerable<User>> GetSuggestionsAtUsersAsync(string q, int type = 0, int count = 10, int range = 2) {
            var json = await HttpHelper.GetBasicCommandAsync("search/suggestions/at_users", new WeiboParameter("q", q), new WeiboParameter("count", count), new WeiboParameter("type", type), new WeiboParameter("range", range));
            return JsonConvert.DeserializeObject<IEnumerable<User>>(json);
        }
        #endregion
    }
}
